package org.gruposp2p.dnie.client.ui.dialogs;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;

import org.gruposp2p.dnie.client.ui.panel.MainPanel;

public class AuthenticateDialogBox extends DialogBox {

	public AuthenticateDialogBox () {
        ensureDebugId("AuthenticateDialogBox");
        showEnterDialog();
        this.setPopupPosition(
                (RootPanel.get().getOffsetWidth() -this.getOffsetWidth() )/2,
                (RootPanel.get().getOffsetHeight()-this.getOffsetHeight())/2
            );
	}
	
	private void showEnterDialog () {
		setText(MainPanel.constants.acceptButtonText());
		FlexTable layout = new FlexTable(); 
		layout.clear();
	    layout.setCellSpacing(6);
	    layout.setWidth("250px");
	    FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
	    layout.setHTML(0, 0, MainPanel.constants.AuthenticateDialogEmailLabel());
	    TextBox emailTextBox = new TextBox ();
	    layout.setWidget(0, 1, emailTextBox);
	    layout.setHTML(1, 0, "Contraseña:");
	    layout.setWidget(1, 1, new TextBox()); 
	    HTML text0 = new HTML(
	    		MainPanel.constants.AuthenticateDialogRegisterText());
	    text0.addClickHandler( new ClickHandler() {
	          public void onClick(ClickEvent event) {
	        	  showRegisterDialog();
	          }
	        });
	    layout.setWidget(2, 0, text0);
	    cellFormatter.setColSpan(2, 0, 2);
	    cellFormatter.setHorizontalAlignment(2, 0,
	        HasHorizontalAlignment.ALIGN_LEFT);

	    // Add a close button at the bottom of the dialog
	    Button cancelButton = new Button(
	    		MainPanel.constants.AuthenticateDialogCancel(),
	        new ClickHandler() {
	          public void onClick(ClickEvent event) {
	            hide();
	          }
	        });
	    Button acceptButton = new Button(
	    		MainPanel.constants.acceptButtonText(),
		        new ClickHandler() {
		          public void onClick(ClickEvent event) {
		            hide();
		          }
		        });
	    HorizontalPanel hPanel = new HorizontalPanel();
	    hPanel.setSpacing(5);
	    hPanel.add(cancelButton);
	    hPanel.add(acceptButton);

	    layout.setWidget(3, 1, hPanel);
	    cellFormatter.setHorizontalAlignment(3, 1, HasHorizontalAlignment.ALIGN_RIGHT);
	    setWidget(layout);
	    //To solve Firefox strange effect
	    emailTextBox.setFocus(true);
	}
	
	private void showRegisterDialog () {
		setText(MainPanel.constants.AuthenticateDialogBoxRegisterCaption());
		FlexTable layout = new FlexTable(); 
	    layout.setCellSpacing(6);
	    layout.setWidth("250px");
	    FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
	    layout.setHTML(0, 0, 
	    		MainPanel.constants.AuthenticateDialogNameLabel());
	    layout.setWidget(0, 1, new TextBox());
	    layout.setHTML(1, 0,  
	    		MainPanel.constants.AuthenticateDialogSurnameLabel());
	    layout.setWidget(1, 1, new TextBox());
	    layout.setHTML(2, 0,  
	    		MainPanel.constants.AuthenticateDialogEmailLabel());
	    TextBox emailTextBox = new TextBox ();
	    layout.setWidget(2, 1, emailTextBox);	    
	    layout.setHTML(3, 0,  
	    		MainPanel.constants.AuthenticateDialogPassword());
	    layout.setWidget(3, 1, new TextBox());	 
	    layout.setHTML(4, 0,  
	    		MainPanel.constants.AuthenticateDialogRepeatPassword());
	    layout.setWidget(4, 1, new TextBox());	 
   

	    // Add a close button at the bottom of the dialog
	    Button cancelButton = new Button(
	    		MainPanel.constants.AuthenticateDialogCancel(),
	        new ClickHandler() {
	          public void onClick(ClickEvent event) {
	        	  showEnterDialog();
	          }
	        });
	    Button acceptButton = new Button(
	    		MainPanel.constants.acceptButtonText(),
		        new ClickHandler() {
		          public void onClick(ClickEvent event) {
		        	  showRegisteredialog();
		          }
		        });
	    HorizontalPanel hPanel = new HorizontalPanel();
	    hPanel.setSpacing(5);
	    hPanel.add(cancelButton);
	    hPanel.add(acceptButton);
	    layout.setWidget(5, 1, hPanel);
	    cellFormatter.setHorizontalAlignment(5, 1, HasHorizontalAlignment.ALIGN_RIGHT);
	    setWidget(layout);
	    //To solve Firefox strange effect
	    emailTextBox.setFocus(true);
	}
	
	private void showRegisteredialog () {
		setText(MainPanel.constants.AuthenticateDialogBoxRegisterCaption());
		FlexTable layout = new FlexTable(); 
		FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
		layout.clear();
	    layout.setCellSpacing(6);
	    layout.setWidth("250px");	
	    HTML textRegistered = new HTML(
	    		MainPanel.constants.AuthenticateDialogRegisteredText());
	    layout.setWidget(0, 0, textRegistered);
	    cellFormatter.setColSpan(0, 0, 2);
	    cellFormatter.setHorizontalAlignment(2, 0,
	        HasHorizontalAlignment.ALIGN_LEFT);	
	    
	    Button acceptButton = new Button(
	    		MainPanel.constants.acceptButtonText(),
		        new ClickHandler() {
		          public void onClick(ClickEvent event) {
		        	  showEnterDialog();
		          }
		        });
	    HorizontalPanel hPanel = new HorizontalPanel();
	    hPanel.setSpacing(5);
	    hPanel.add(acceptButton);
	    layout.setWidget(1, 1, hPanel);
	    cellFormatter.setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_RIGHT);
	    setWidget(layout);
	}

}
